namespace MTOM_Library
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.IO;
    using System.Threading;
    using System.Windows.Forms;

    public class FileTransferUpload : FileTransferBase
    {
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            try
            {
                base.OnDoWork(e);
                base.Offset = long.Parse(e.Argument.ToString());
                int num = 0;
                base.LocalFileName = Path.GetFileName(base.LocalFilePath);
                if (base.AutoSetChunkSize)
                {
                    base.ChunkSize = 0x4000;
                }
                if (!File.Exists(base.LocalFilePath))
                {
                    throw new Exception(string.Format("文件{0}不存在", base.LocalFilePath));
                }
                long length = new FileInfo(base.LocalFilePath).Length;
                string str = FileTransferBase.CalcFileSize(length);
                byte[] buffer = new byte[base.ChunkSize];
                using (FileStream stream = new FileStream(base.LocalFilePath, FileMode.Open, FileAccess.Read))
                {
                    int num3;
                    stream.Position = base.Offset;
                    do
                    {
                        num3 = stream.Read(buffer, 0, base.ChunkSize);
                        if (num3 != buffer.Length)
                        {
                            base.ChunkSize = num3;
                            byte[] destinationArray = new byte[num3];
                            Array.Copy(buffer, destinationArray, num3);
                            buffer = destinationArray;
                        }
                        if (buffer.Length == 0)
                        {
                            if (base.IncludeHashVerification)
                            {
                                base.ReportProgress(0x63, "检查文件...");
                                base.HashThread = new Thread(new ThreadStart(this.CheckLocalFileHash));
                                base.HashThread.Start();
                                base.WebService.Timeout = 0x927c0;
                                base.RemoteFileHash = base.WebService.CheckFileHash(base.LocalFileName);
                                base.WebService.Timeout = 0x7530;
                                base.HashThread.Join();
                                if (base.LocalFileHash == base.RemoteFileHash)
                                {
                                    e.Result = string.Format("Hashes match exactly\r\nLocal hash:  {0}\r\nServer hash: {1}", base.LocalFileHash, base.RemoteFileHash);
                                }
                                else
                                {
                                    e.Result = string.Format("Hashes are different!\r\nLocal hash:  {0}\r\nServer hash: {1}", base.LocalFileHash, base.RemoteFileHash);
                                }
                            }
                        }
                        try
                        {
                            base.WebService.AppendChunk(base.LocalFileName, buffer, base.Offset);
                            if (base.AutoSetChunkSize)
                            {
                                switch ((num % base.ChunkSizeSampleInterval))
                                {
                                    case 0:
                                        base.StartTime = DateTime.Now;
                                        break;

                                    case 1:
                                        base.CalcAndSetChunkSize();
                                        buffer = new byte[base.ChunkSize];
                                        break;
                                }
                            }
                            base.Offset += num3;
                            string userState = string.Format("速度: {0} / {1}", FileTransferBase.CalcFileSize(base.Offset), str);
                            base.ReportProgress((int)((base.Offset / length) * 100M), userState);
                        }
                        catch (Exception exception)
                        {
                            Debug.WriteLine("Exception: " + exception.ToString());
                            stream.Position -= num3;
                            if (base.NumRetries++ >= base.MaxRetries)
                            {
                                stream.Close();
                                throw new Exception(string.Format("Error occurred during upload, too many retries. \n{0}", exception.ToString()));
                            }
                        }
                        num++;
                    }
                    while ((num3 > 0) && !base.CancellationPending);
                }                
            }
            catch (Exception exception2)
            {
                MessageBox.Show("联机远程服务器发送错误:\n\r" + exception2.Message + "\n\r请检查远程服务器的防火墙设置！以及杀毒软件的文件监控是否关闭！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }



        public void RunWorkerSync(DoWorkEventArgs e)
        {
            this.OnDoWork(e);
            base.OnRunWorkerCompleted(new RunWorkerCompletedEventArgs(e.Result, null, false));
        }
    }
}

